package cn.dgiot.fd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dgiot.fd.dao.IPropertiesSetFlowDAO;
import cn.dgiot.fd.model.DeviceStateModel;
import cn.dgiot.fd.model.PropertiesSetFlowModel;
import cn.dgiot.fd.service.IDeviceStateService;
import cn.dgiot.fd.service.IPropertiesSetFlowService;

/**
 * [属性设置流水]
 * @author
 *
 */
@Service
public class PropertiesSetFlowServiceImpl implements IPropertiesSetFlowService{
 
	@Autowired
	private IPropertiesSetFlowDAO propertiesSetFlowDAO;

	@Autowired
	private IDeviceStateService deviceStateService;
	
	@Override
	public void insertPropertiesSet(PropertiesSetFlowModel propertiesSetFlowModel) {
		List<String> deviceNames = propertiesSetFlowModel.getDeviceNames();
		List<PropertiesSetFlowModel> propertiesSetModelList = new ArrayList<PropertiesSetFlowModel>();
		for(int i=0;i<deviceNames.size();i++) {
			String deviceName = deviceNames.get(i);
			DeviceStateModel deviceStateModel = new DeviceStateModel();
	    	deviceStateModel.setDeviceName(deviceName);
	    	deviceStateModel.setProductKey(propertiesSetFlowModel.getProductKey());
	    	DeviceStateModel deviceStateModelResult = deviceStateService.getDeviceState(deviceStateModel);
			PropertiesSetFlowModel param = new PropertiesSetFlowModel();
			param.setIotId(deviceStateModelResult.getIotId());
			param.setDeviceName(deviceName);
			param.setProductKey(propertiesSetFlowModel.getProductKey());
			param.setMessageId(propertiesSetFlowModel.getMessageId());
			param.setStatus(propertiesSetFlowModel.getStatus());
			param.setCreateTime(propertiesSetFlowModel.getCreateTime());
			param.setId(UUID.randomUUID().toString().replace("-", ""));
			propertiesSetModelList.add(param);
		} 
		propertiesSetFlowDAO.insertPropertiesSet(propertiesSetModelList);
	}

	@Override
	public void updatePropertiesSetStatus(PropertiesSetFlowModel propertiesSetFlowModel) {
		// TODO Auto-generated method stub
		propertiesSetFlowDAO.updatePropertiesSetStatus(propertiesSetFlowModel);
	}

}
